package persistent.impl;

import java.sql.ResultSet;
import java.util.List;

import basic.BaseDAO;
import model.TReview;
import model.VReview;
import persistent.dao.ReviewDAO;

public class ReviewDAOImpl implements ReviewDAO {
	BaseDAO base=null;
	
	public ReviewDAOImpl(){
		base=new BaseDAO();
	}
	@Override
	public int addReview(TReview review) {
		String proname="up_addReview(?,?,?,?)";
		Object para[]={review.getForumid(),review.getReviewcontent(),
		review.getUserid(),review.getIpaddr()};
		int row =(Integer)base.excuteSelectProduce(proname, para);
		if(row!=0){
			return row;
		}
		else {
			return 0;
		}
		
	}

	@Override
	public boolean deleteReivew(TReview review) {
		String proname="up_deleteReview(?,?)";
		Object para[]={review.getReviewid(),review.getForumid()};
		int row = (Integer)base.excuteSelectProduce(proname, para);
		if(row!=0){
			return true;
		}
		else {
			return false;
		}
		
	}

	@Override
	public boolean deleteReviewsBelongForum(int forumid) {
		String proname="up_deleteAllReview(?)";
		Object para[]={forumid};
		int row = (Integer)base.excuteSelectProduce(proname, para);
		if(row!=0){
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public List<VReview> getReviewListByForumId(int forumid) {
		String sql="select * from V_Review where forumid=? order by createtime desc";
		Object para[]={forumid};
		ResultSet rs = base.select(sql, para);
		List<VReview> list=VReview.toList(rs);
		base.close();
		return list;
	}

	@Override
	public VReview getReviewById(int reviewid) {
		String sql ="select * from V_Review where reviewid=?";
		Object para[]={reviewid};
		ResultSet rs = base.select(sql, para);
		VReview vr=new VReview(rs);
		base.close();
		return vr;
	}

}
